import { Effect } from 'dva';
import { Reducer } from 'redux';

import { pftCompany, pftBuy, pftSale } from '@/services/profit';
import { notification } from 'antd';

export interface CurrentUser {
  avatar?: string;
  name?: string;
  title?: string;
  group?: string;
  signature?: string;
  tags?: {
    key: string;
    label: string;
  }[];
  userid?: string;
  unreadCount?: number;
}

export interface State {
  total?: number;
  isRefresh?: boolean;
  list?: Array<any>;
  view?: {};
  totalprofit?: {};
}

export interface UserModelType {
  namespace: 'personsale';
  state: State;
  effects: {
    add: Effect;
    edit: Effect;
    delete: Effect;
    list: Effect;
    view: Effect;
  };
  reducers: {
    redList: Reducer<State>;
    reFresh: Reducer<State>;
    redView: Reducer<State>;
  };
}

const UserModel: UserModelType = {
  namespace: 'personsale',

  state: {
    total: 0,
    isRefresh: true,
    list: [],
    view: {},
    totalprofit: {},
  },

  effects: {
    *add({ payload }, { call, put }) {
      try {
        const response = yield call(pftCompany, payload);
        notification.success({
          message: '用户新增成功',
          description: '',
        });
        yield put({
          type: 'reFresh',
        });
      } catch (error) {}
    },
    *edit({ payload }, { call, put }) {
      try {
        const response = yield call(pftBuy, payload);
        notification.success({
          message: '用户修改成功',
          description: '',
        });
        yield put({
          type: 'reFresh',
        });
      } catch (error) {}
    },
    *delete({ payload }, { call, put }) {
      try {
        const response = yield call(pftSale, payload.dictId);
        notification.success({
          message: '用户删除成功',
          description: '',
        });
        yield put({
          type: 'reFresh',
        });
      } catch (error) {}
    },
    *list({ payload }, { call, put }) {
      try {
        const response = yield call(pftSale, payload);
        const data = response.data.content;
        let result: Array<any> = [];
        data.forEach((element: any) => {
          result = result
            .concat([
              {
                ...element.profitsResultUnitVos[0],
                ...element.totalSummaryVo,
                length: element.profitsResultUnitVos.length,
              },
            ])
            .concat(element.profitsResultUnitVos.slice(1));
        });
        yield put({
          type: 'redList',
          payload: result,
          total: response.data.totalElements,
          totalprofit: response.data.totalSummaryVo,
        });
      } catch (error) {
        console.log(error);
      }
    },
    *view({ payload }, { call, put }) {
      console.log(payload, 999999999999999);
      yield put({
        type: 'redView',
        payload: payload,
      });
    },
  },

  reducers: {
    redList(state, action) {
      return {
        ...state,
        list: action.payload,
        isRefresh: false,
        total: action.total,
        totalprofit: action.totalprofit,
      };
    },
    redView(state, action) {
      return {
        ...state,
        view: action.payload,
      };
    },
    reFresh(state, action) {
      return {
        ...state,
        isRefresh: true,
      };
    },
  },
};

export default UserModel;
